package com.xps.and.yuntong.Utils;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by m on 2017/3/3.
 */

public class RegularValidation {

    /**
     * 验证邮箱格式是否正确
     */
    public static boolean isValidEmail(String mail) {
        Pattern pattern = Pattern
                .compile("^[A-Za-z0-9][\\w\\._]*[a-zA-Z0-9]+@[A-Za-z0-9-_]+\\.([A-Za-z]{2,4})");
        Matcher mc = pattern.matcher(mail);
        return mc.matches();
    }

    /**
     * 验证手机格式
     */
    public static boolean isMobileNO(String mobiles) {
        Pattern p = Pattern.compile("^((13[0-9])|(15[^4,\\D])|(17[0678])|(18[0,2,5-9]))\\d{8}$");
        Matcher m = p.matcher(mobiles);
        return m.matches();
    }

    /**
     * 验证日期格式
     */
    public static boolean isData(final String date) {
        Pattern pattern = Pattern.compile("((19|20)[0-9]{2})(-|.)(0?[1-9]|1[012])(-|.)(0?[1-9]|[12][0-9]|3[01])");
        Matcher matcher = pattern.matcher(date);
        if (!matcher.matches()) {
            pattern = Pattern.compile("(0?[1-9]|1[012])(-|.)(0?[1-9]|[12][0-9]|3[01])(-|.)((19|20)[0-9]{2})");
            matcher = pattern.matcher(date);
            return matcher.matches();
        }
        return true;
    }

    /**
     * 校验银行卡卡号
     *
     * @param cardId
     * @return
     */
    public static boolean checkBankCard(String cardId) {
        char bit = getBankCardCheckCode(cardId.substring(0, cardId.length() - 1));
        return cardId.charAt(cardId.length() - 1) == bit;
    }

    /**
     * 从不含校验位的银行卡卡号采用 Luhm 校验算法获得校验位
     *
     * @param nonCheckCodeCardId
     * @return
     */
    public static char getBankCardCheckCode(String nonCheckCodeCardId) {
        if (nonCheckCodeCardId == null || nonCheckCodeCardId.trim().length() == 0
                || !nonCheckCodeCardId.matches("\\d+")) {
            throw new IllegalArgumentException("Bank card code must be number!");
        }
        char[] chs = nonCheckCodeCardId.trim().toCharArray();
        int luhmSum = 0;
        for (int i = chs.length - 1, j = 0; i >= 0; i--, j++) {
            int k = chs[i] - '0';
            if (j % 2 == 0) {
                k *= 2;
                k = k / 10 + k % 10;
            }
            luhmSum += k;
        }
        return (luhmSum % 10 == 0) ? '0' : (char) ((10 - luhmSum % 10) + '0');
    }

    /**
     * 验证输入的身份证号是否合法
     */
    public static boolean isLegalId(String id) {
        if (id.toUpperCase().matches("(^\\d{15}$)|(^\\d{17}([0-9]|X)$)")) {
            return true;
        } else {
            return false;
        }
    }
//        if (name.contains("·") || name.contains("•")){
//            if (name.matches("^[\\x80-\\xff]{4,12}$")){
//                return true;
//            }else {
//                return false;
//            }
//        }else {

    /**
     * 验证输入的名字是否为“中文”或者是否包含“·”
     */
    /**
     * 验证输入的名字是否为“中文”或者是否包含“·”
     */
//    public static boolean isLegalName(String name){
//
//            if (name.matches("^[\\u4e00-\\u9fa5]+$")){
//                return true;
//            }else {
//                return false;
//
//        }
//    }
    public static boolean isLegalName(String name){

            if (name.matches("^[\\u4e00-\\u9fa5]+$")){
                return true;
            }else {
                return false;

        }
    }
    }
